package com.study.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/Servlet06")
public class Servlet06 extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 同时设置服务端和客户端的响应格式
		resp.setContentType("text/html;charset=utf-8");
		
		Cookie cookie = new Cookie("name", "zhangsan");
		cookie.setPath("/");
		resp.addCookie(cookie);
		
		Cookie[] cookies = req.getCookies();
		if (cookies != null) {
			for (Cookie c : cookies) {
				System.out.println(c.getName() + " : " + c.getValue());
			}
		}
		
		resp.getWriter().write("cookie添加成功！");
	}
}
